package com.gxuwz.Market.business.dao;

import org.springframework.stereotype.Repository;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gxuwz.Market.business.entity.ServiceBranch;
import com.gxuwz.core.dao.impl.BaseDaoImpl;
import com.gxuwz.core.pagination.Result;



@Repository("serviceBranchDAO")
public class ServiceBranchDAO extends BaseDaoImpl<ServiceBranch>{
	/**
	 * 根据条件查找分页
	 * @param ServiceBranch 模型
	 * @param page 第几页
	 * @param row 长度
	 * @return
	 * @author 李宪乾
	 * @date 2017.9.27
	 */
	@SuppressWarnings("unchecked")
	public Result<ServiceBranch> find(ServiceBranch serviceBranch, int page, int size){
		String queryString = "from ServiceBranch where 1=1";
		if(null != serviceBranch.getId() && !"".equals(serviceBranch.getId())){			
			queryString = queryString + "and id like '%"+serviceBranch.getId()+"%'";
		}
		if(null != serviceBranch.getParentId() && !"".equals(serviceBranch.getParentId())){
			queryString = queryString +" and parentId like '%"+serviceBranch.getParentId()+"%'";
		}
		if(null != serviceBranch.getSonId() && !"".equals(serviceBranch.getSonId())){
			queryString = queryString +" and sonId like '%"+serviceBranch.getSonId()+"%'";
		}
		int start=(page-1)*size;
		int limit =size;
		return (Result<ServiceBranch> )super.find(queryString, null, null, start, limit);
	}
	
	public List<ServiceBranch> findAll() {
		List<ServiceBranch> ServiceBranchList = null;
		String queryString = "from ServiceBranch where 1=1";
		ServiceBranchList = this.getHibernateTemplate().find(queryString);
		return ServiceBranchList;
	}
	/**
	 * 根据id查询单个用户
	 * @param userId
	 * @return
	 */
	public ServiceBranch findById(int id){
		List<ServiceBranch> ServiceBranchList = null;
		String queryString = "from ServiceBranch where id ="+id;
		try{
			ServiceBranchList = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return ServiceBranchList.get(0);
	}
	
	public List<ServiceBranch> findByProperty(String property, String value){
		List<ServiceBranch> list = null;
		String queryString = "from ServiceBranch where "+property+" ='"+value+"' ";
		try{
			list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
	
	public List<ServiceBranch> findByProperty2(String property, String value,String property2, String value2){
		List<ServiceBranch> list = null;
		String queryString = "from ServiceBranch where "+property+" ='"+value+"' and "+property2+" ='"+value2+"'";
		try{
			list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
	
	
}